package me.tuyou.mybatis.test.interfaces.plugin;

import lombok.extern.log4j.Log4j;
import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.plugin.*;
import org.apache.ibatis.session.ResultHandler;

import java.sql.Statement;
import java.text.MessageFormat;
import java.util.Properties;

@Intercepts({
        @Signature(type = StatementHandler.class, method = "query", args = {Statement.class, ResultHandler.class})
})
@Log4j
public class LogPlugin implements Interceptor {
    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        log.info(MessageFormat.format("开始执行,target={0},method={1},args={2}",
                invocation.getTarget().getClass().getSimpleName(),
                invocation.getMethod().getName(),
                invocation.getArgs()));

        Object obj = invocation.proceed();

        log.info(MessageFormat.format("结束执行,target={0},method={1},args={2}",
                invocation.getTarget().getClass().getSimpleName(),
                invocation.getMethod().getName(),
                invocation.getArgs()));
        return obj;
    }

    @Override
    public Object plugin(Object target) {
        return Plugin.wrap(target, this);
    }

    @Override
    public void setProperties(Properties properties) {
        log.info("properties:" + properties);
    }
}
